<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="套餐名称" prop="comboName">
            <el-input v-model="formData.comboName" placeholder="请输入套餐名称" :clearable="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收费方式" prop="chargeType">
            <el-select v-model="formData.chargeType" placeholder="请输入收费方式" :clearable="true">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.FEE_CHARGE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="价格" prop="price">
            <InputNumber v-model="formData.price" placeholder="请输入价格" type="decimal" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="formData.remark"
              placeholder="请输入备注"
              rows="3"
              :clearable="true"
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="图片" prop="imgUrl">
            <UploadImg v-model="formData.imgUrl" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="RestaurantComboEdit" lang="ts">
import * as RestaurantRecipeApi from '@/api/restaurant/recipe'
import * as RestaurantComboModel from '@/model/restaurant/combo/RestaurantCombo'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
const { t } = useI18n()
const message = useMessage()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
let formType = ''
const formData = ref({
  id: '',
  comboName: '',
  chargeType: 1,
  price: 0,
  remark: '',
  imgUrl: ''
})
const formRules = reactive({
  comboName: [{ required: true, message: '请输入套餐名称', trigger: 'blur' }],
  chargeType: [{ required: true, message: '请选择收费方式', trigger: 'change' }]
})
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RestaurantRecipeApi.comboGet(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: '',
    comboName: '',
    chargeType: 1,
    price: 0,
    remark: '',
    imgUrl: ''
  }
  formRef.value?.resetFields()
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RestaurantComboModel.RestaurantCombo
    if (formType === 'create') {
      await RestaurantRecipeApi.comboCreate(data)
      message.success(t('common.createSuccess'))
    } else {
      await RestaurantRecipeApi.comboUpdate(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
